using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace LumiSoft.Net
{
    /// <summary>
    /// Common utility methods.
    /// </summary>
    public class Net_Utils
    {
        #region static method GetLocalHostName

        /// <summary>
        /// Gets local host name or argument <b>hostName</b> value if it's specified.
        /// </summary>
        /// <param name="hostName">Host name or null.</param>
        /// <returns>Returns local host name or argument <b>hostName</b> value if it's specified.</returns>
        public static string GetLocalHostName(string hostName)
        {
            if(string.IsNullOrEmpty(hostName)){
                return System.Net.Dns.GetHostName();
            }
            else{
                return hostName;
            }
        }

        #endregion

        #region static method CompareArray

        /// <summary>
        /// Compares if specified array itmes equals.
        /// </summary>
        /// <param name="array1">Array 1.</param>
        /// <param name="array2">Array 2</param>
        /// <returns>Returns true if both arrays are equal.</returns>
        public static bool CompareArray(Array array1,Array array2)
        {
            if(array1 == null && array2 == null){
                return true;
            }
            if(array1 == null && array2 != null){
                return false;
            }
            if(array1 != null && array2 == null){
                return false;
            }            
            if(array1.Length != array2.Length){
                return false;
            }
            else{
                for(int i=0;i<array1.Length;i++){
                    if(!array1.GetValue(i).Equals(array2.GetValue(i))){
                        return false;
                    }
                }
            }

            return true;
        }

        #endregion

        #region static method StreamCopy

        /// <summary>
        /// Copies <b>source</b> stream data to <b>target</b> stream.
        /// </summary>
        /// <param name="source">Source stream. Reading starts from stream current position.</param>
        /// <param name="target">Target stream. Writing starts from stream current position.</param>
        /// <param name="blockSize">Specifies transfer block size in bytes.</param>
        /// <returns>Returns number of bytes copied.</returns>
        public static long StreamCopy(Stream source,Stream target,int blockSize)
        {
            if(source == null){
                throw new ArgumentNullException("source");
            }
            if(target == null){
                throw new ArgumentNullException("target");
            }
            if(blockSize < 1024){
                throw new ArgumentException("Argument 'blockSize' value must be >= 1024.");
            }

            byte[] buffer      = new byte[blockSize];
            long   totalReaded = 0;            
            while(true){
                int readedCount = source.Read(buffer,0,buffer.Length);
                // We reached end of stream, we readed all data sucessfully.
                if(readedCount == 0){
                    return totalReaded;
                }
                else{
                    target.Write(buffer,0,readedCount);
                    totalReaded += readedCount;
                }
            }
        }

        #endregion

    }
}
